$(function () {
    var layer = layui.layer
    var form = layui.form
    var laypage = layui.laypage
    var adSizeList = []
    var q = {
        advposdesc: $('[name=advimgdesc]').val(),
        advposname:$('[name=advimgname]').val(),
        page:'1',
        limit: '',
    }
    initAdvPosList()
    function initAdvPosList() {
        console.log('ok');
        $.ajax({
            method: 'GET',
            url: 'http://124.223.14.236:8060/admin/ydd_advpos/list',
            headers : {
                'x-token': "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ3ODY5MzZ9.VFu0p1w6gPzm9t3w5zDqnciWVSbFG6ZIz3ReIC2ylEQ"
            },
            data: {
                advposdesc: $('[name=advimgdesc]').val(),
                advposname:$('[name=advimgname]').val(),
                page:'',
                limit: '',
            },
            success: function (res) {
                /* if (res.status !== 200) {
                    return console.log('获取失败!');
                } */
                console.log(res);
                var htmlStr = template('tpl_advpos', res)
                $('tbody').html(htmlStr)
                renderPage(res.data.count)
            }
        })
    }
    function renderPage(total) {        
        laypage.render({
            elem: 'page-box' //注意，这里的 test1 是 ID，不用加 # 号
            ,count: total //数据总数，从服务端得到
            , limit: q.page, //设置当前页码位置 防止重新渲染回到起始首页
            curr: q.limit,
            limits:[2,3,4,5,6],
            layout:	['count','limit','prev', 'page', 'next','skip'],
            jump: function(obj, first){
                //obj包含了当前分页的所有参数，比如：
                console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                console.log(obj.limit); //得到每页显示的条数
                q.limit = obj.curr 
                q.page = obj.limit
               
                //首次不执行
                if(!first){
                  //do something
                  initAdvList()
                }
              }
        });
    }

    $('tbody').on('click', '#btnDel', function () {
        console.log('ok', $(this).attr('data-id'));
        var id = $(this).attr('data-id') //为删除按钮添加自定义属性 并获取自定义属性 拿到数据 
        //二次确认删除弹出框
        layer.confirm('确定要删除吗?', {icon: 3, title:'提示'}, function(index){
            //do something
            $.ajax({
                url: 'http://124.223.14.236:8060/admin/ydd_advimg/del',
                method: 'GET',
                headers : {
                    'x-token': "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ3ODY5MzZ9.VFu0p1w6gPzm9t3w5zDqnciWVSbFG6ZIz3ReIC2ylEQ"
                },
                data: {
                    id:id
                },
                success: function (res) {
                    if (res.status !== 0) {
                        return layer.msg(res.errmsg)
                    }
                    layer.msg('删除成功!')
                    initAdvList()
                }
                
            })
            layer.close(index);
          });
       
    })
    var form = layui.form
    $('tbody').on('click', '#editBtn', function () {
        //var cate_id = null
        console.log('点击了编辑按钮');
        layerindex = layer.open({
            type: 1,
            area: ['600px', '400px'],
            title: '修改广告位'
            , content: $('#tpl-EditPos').html()
        });
        var id = $(this).attr('data-id')
        console.log(id);
        $.ajax({
            method: 'GET',
            url: 'http://124.223.14.236:8060/admin/ydd_advpos/rowInfo',
            headers : {
                'x-token': "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ3ODY5MzZ9.VFu0p1w6gPzm9t3w5zDqnciWVSbFG6ZIz3ReIC2ylEQ"
            },
            data: {
                id:id
            },
            success: function (res) {
               /*  if (res.status !== 0) {
                    return layer.msg(res.message)
                } */
           //快速给form表单赋值   
            form.val('formedit', res.data)
            $('#select select').append(new Option(item.advimgpos,res.data.id))
           if(adSizeList.length > 0){
            $.each(adSizeList, function (index, item) {
              adSizeList = res.data
            $('#select select').append(new Option(item.advposname,item.id))
            })
            form.render('select')
        }                    
            }
        })
             
    })
      
})